<?php

class Career_Opportunities extends Controller {

    function Career_Opportunities()
    {
        parent::Controller();    
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('careeropportunitiesmodel');  
        $this->load->library("form_validation");       
    }     
    
    function index()
    {

        $data["list"] = $this->careeropportunitiesmodel->admin_get_list();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'career_opportunities'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/career_opportunities/career_opportunities_list', $data);   
        $this->load->view('admin/default/footer');   

    }
 
    function edit($id = NULL)
    {
        if($id == NULL) show_404();

        $data["item"] = $this->careeropportunitiesmodel->admin_get_item($id); 
        if(empty($data["item"])) show_404();

        $this->_process_form();

        $data["action"] = "update";
        $data["token"] = generate_token("career_opportunities",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'career_opportunities'));
        $this->load->view('admin/career_opportunities/career_opportunities_form', $data);   
        $this->load->view('admin/default/footer');   

    }
       
    function add()
    {
        $this->_process_form();

        $data["item"] = array(
                "id_Career_Opportunities" => "",   
                "Title_Career_Opportunities" => "",     
                "Description_Career_Opportunities" => ""
        ); 

        $data["action"] = "create";
        $data["token"] = generate_token("career_opportunities",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'career_opportunities'));
        $this->load->view('admin/career_opportunities/career_opportunities_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->careeropportunitiesmodel->admin_get_item($id, "id_Career_Opportunities as id, Title_Career_Opportunities as title, Description_Career_Opportunities as description, Approved_Career_Opportunities as approved, Active_Career_Opportunities as active ");

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/career_opportunities');

        } else {

            $data["controller"] = "career_opportunities";
            $this->load->model('careeropportunitiesmodel');

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'career_opportunities'));
            $this->load->view('admin/career_opportunities/career_opportunities_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }  
    function preview($id = NULL)
    {

        if($this->input->post("submit_action") === FALSE){

            $data["item"] = $this->careeropportunitiesmodel->admin_get_item($id, "id_Career_Opportunities as id, Title_Career_Opportunities as title, Description_Career_Opportunities as description, Approved_Career_Opportunities as approved, Active_Career_Opportunities as active "); 
            if (empty($data["item"])) show_404();             

            $data["action"] = "view";

        } else {

            $data["item"] = array(
                'id' => $this->input->post("id", TRUE),      
                'title' => $this->input->post("title", TRUE),        
                'description' => $this->input->post("description", TRUE));

            $data["action"] = "preview";

        }

        $data["controller"] = "career_opportunities";   
        $this->load->view('admin/previews/preview_career_opportunities_template', $data);   

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->careeropportunitiesmodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/career_opportunities');

        } else {

            $this->careeropportunitiesmodel->admin_delete($id);
            set_response('success', "The item <i>".$data["item"]["Title_Career_Opportunities"]."</i> was deleted.");
            redirect('admin/career_opportunities');
        }
    }
    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Career_Opportunities' => 0);
        $this->careeropportunitiesmodel->admin_update($id, $data);
        $item = $this->careeropportunitiesmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Career_Opportunities"].'</i> is now inactive. <a href="'.site_url("admin/career_opportunities/preview/".$item["id_Career_Opportunities"]).'" rel="view">Click to view</a>');
        redirect('admin/career_opportunities');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Career_Opportunities' => 1);
        $this->careeropportunitiesmodel->admin_update($id, $data);
        $item = $this->careeropportunitiesmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Career_Opportunities"].'</i> is now active. <a href="'.site_url("admin/career_opportunities/preview/".$item["id_Career_Opportunities"]).'" rel="view">Click to view</a>');
        redirect('admin/career_opportunities');
    }
    function approve($id = false){

        if(!is_allowed(array(ROLE_ADMIN,ROLE_PUBLISHER))) show_404();
        if($id == NULL) show_404();  

        $data = array('Approved_Career_Opportunities' => 1, 'Active_Career_Opportunities' => 1);
        $this->careeropportunitiesmodel->admin_update($id, $data);
        $item = $this->careeropportunitiesmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Career_Opportunities"].'</i> was approved. <a href="'.site_url("admin/career_opportunities/preview/".$item["id_Career_Opportunities"]).'" rel="view">Click to view</a>');
        redirect('admin/career_opportunities');
    }
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("career_opportunities",$this->input->post("token",TRUE))){

                    $data = array(
                        'Title_Career_Opportunities' => $this->input->post("title"),                
                        'Description_Career_Opportunities' => $this->input->post("description"),
                    );

                    switch($this->input->post("submit_action")){    

                        case 'create':

                            $new_id = $this->careeropportunitiesmodel->admin_insert($data);
                            set_response('success','The item <i>'.$data['Title_Career_Opportunities'].'</i> was created. <a href="'.site_url("admin/career_opportunities/preview/".$new_id).'" rel="view">Click to view</a>');

                        break;
                        case 'update':

                            $this->careeropportunitiesmodel->admin_update($this->input->post("id"), $data);
                            set_response('success','The item <i>'.$data['Title_Career_Opportunities'].'</i> was updated. <a href="'.site_url("admin/career_opportunities/preview/".$this->input->post("id")).'" rel="view">Click to view</a>');

                        break;        

                    }

                    redirect("admin/career_opportunities");

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config = array( 
                        array(
                         'field'   => 'title',
                         'label'   => 'Title',
                         'rules'   => 'trim|required|xss_clean|max_length[250]'
                        ), 
                        array(
                         'field'   => 'description',
                         'label'   => 'Description',
                         'rules'   => 'trim|required|xss_clean'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */